<!--
/*************************************************************************
* Copyright (c) 2010 im007boy@gmail.com. All rights reserved.            *
* Use of this source code is governed by a BSD-style license that can be *
* found in the LICENSE file.                                             *
*************************************************************************/
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>RSS高级同步</title>
<script type="text/javascript" src="./js/userdata.js"></script>

<script type="text/javascript">
function output_rss_list(){
	var i,len;
	len = rssinfo.site.length;
	document.getElementById("rss_list").innerHTML = "";
	for (i = 0;i < len;i++)
	{
		document.getElementById("rss_list").innerHTML += '<li><span class="rss_name" title="'+ rssinfo.site[i].feedurl +'">'+rssinfo.site[i].name+'</span><span class="btn_delete" title="删除" onclick="del_site('+i+')">X</span></li>'
	}
}
function options_onload(){
	var i = 0;
	rssinfo.site = [];
	rssinfo.cache = [];
	rssinfo.getFromLocalstorage();
	output_rss_list();
	
	//if some information in url
	var title_index = location.href.indexOf('?title=');
	var href_index = location.href.indexOf('&href=');
	if (title_index != -1 && href_index != -1)
	{
		var rss_title = decodeURI(location.href.slice(title_index+7,href_index));
		var rss_href = decodeURI(location.href.substr(href_index+6));
		document.getElementsByName("new_feed_name")[0].value = rss_title;;
		url = document.getElementsByName("new_feed_url")[0].value = rss_href;
	}
	document.getElementsByName("new_feed_style")[0].value = '[title]+[msg: ]+[link]';
	format_preview();
}
function del_site(id){
	var request = {
		'hope':'delete',
		'site':{
			'id':id
		}
	}
	chrome.extension.sendRequest(request,function(response){
		options_onload();
		trackButton('Delete','Accept');
	});	
	trackButton('Delete','Click');
}
function format_check(str){
	if (typeof str != "string")
		str = ""
	var format = str.split('+');
	var pre = {
		'setting':[],
		'str':''
	}
	pre.setting = [];
	for (var i = 0;i < format.length;i++)
	{
		if (format[i].slice(-1) != ']')
			continue;
		switch(format[i])
		{
		case "[title]"://title
			pre.setting.push("title");
			pre.str += "标题"
			break;
		case "[link]"://link
			pre.setting.push("link");
			pre.str += "http://www.neverbest.com"
			break;
		case "[desc]":
			pre.setting.push("desc");
			pre.str += "这是简介。此扩展由im007boy@gmail.com制作，欢迎提出建议。";
			break;
		default:
			if(format[i].slice(0,5) == '[msg:' && format[i].length >6)
			{
				pre.setting.push("msg:"+format[i].slice(5,-1));
				pre.str += format[i].slice(5,-1);
			}
			break;
		}
	}
	//console.log(setting);
	return pre;
}
function format_preview(){
	document.getElementsByName("new_feed_style_preview")[0].innerHTML = "";
	var pre = format_check(document.getElementsByName("new_feed_style")[0].value);
	for (var i= 0;i < pre.setting.length;i++)
		document.getElementsByName("new_feed_style_preview")[0].innerHTML += '<small style="color:gray">[</small><span style="color:#DB2831;">'+pre.setting[i]+'</span><small style="color:gray">]</small>';
	document.getElementsByName("new_feed_style_preview")[0].innerHTML += '<div style="border: 1px solid #CCC;">'+pre.str + '</div>';
}
function add_new_feed(){
	var name = document.getElementsByName("new_feed_name")[0].value;
	var url = document.getElementsByName("new_feed_url")[0].value;
	if (name == '' || url == '')
		return ;
	//format check
	var format = document.getElementsByName("new_feed_style")[0].value;
	var pre = {};
	pre = format_check(format);
	
	var i = 0;
	if (pre.setting.length == 0)
	{	
		alert("请定义格式!");
		return ;
	}
	var msg_str = 0;
	for (i = 0;i < pre.setting.length;i++)
	{
		if (pre.setting[i].substr(0,4) == 'msg:')
			msg_str += pre.setting[i].substr(4);
	}
	if (str_length_limit(msg_str))
	{
		alert("错误！自定义数据太长！");
		return;
	}
	var request = {
		'hope':'add_new_feed',
		'site':{
			'name':name,
			'url':url,
			'feedurl':url,
			'setting':pre.setting
		}
	};
	chrome.extension.sendRequest(request,function(response){
		trackButton('Add','Accept');
		options_onload();
	});
	trackButton('Add','Click');

}
function set_login_time_out(s){
	if(s == '+')
	{
		if (parseInt(localStorage["login_time_out"])>8000)
			localStorage["login_time_out"] = parseInt(localStorage["login_time_out"])+5000;
		else
			localStorage["login_time_out"] = 13000;	
		var msg = "已设置延时"+localStorage["login_time_out"]+"毫秒。";
		/***
		bug~
		if(!oauth.hasToken()){
			msg += "是否立即登录？";
			if(confirm(msg)){
			//rssinfo.logining = 1;
			oauth.authorize(function(){
				//alert("Accepd");
			});	
			}
		}else
			;
		***/	
		document.getElementById("time_out").innerHTML = msg;
	}else{
		localStorage.removeItem("login_time_out");
		document.getElementById("time_out").innerHTML = "已恢复延时8秒";
	}
}
</script>
<script type="text/javascript" src="./js/analytics.js"></script>

<style>
.btn_delete{
	padding-left:5px;
	color:red;
	cursor:pointer;
}
.notice{
	border: 1px solid #CCC;
	border-bottom-left-radius: 3px 3px;
	border-bottom-right-radius: 3px 3px;
	border-top-left-radius: 3px 3px;
	border-top-right-radius: 3px 3px;

}
</style>
</head>
<body onload="options_onload()">
<div class="notice" >
新添加后的feed会被同步。
所有feed每2分钟下载一次。所有feed的数据每3分半同步一次。
为了防止频繁同步被新浪限制，每次每个feed只同步3条数据。其余数据会在下次同步。（新浪限制每小时最多发布30条数据）<br />
注意：这是一个本地化脚本，如果Chrome被关闭，则同步终止。下次Chrome启动时会继续同步。
Chrome关闭期间的数据如果更新太多则可能会有遗漏（多余15条或更新数小于feed输出条数）。<br />
<b>可以先打开某个网站，再点击工具栏图标<img src="./img/icon48.png" style="height:19px; width:19px;" />，本扩展会自动探测页面feed；</b>
</div>
<ul id="rss_list">

</ul>
<div>
<label>标题：<input name="new_feed_name" type="text" size="35"> </label><br/>
<label>feed地址,http://或https://开头：<input name="new_feed_url" type="text" size="35"> </label><br />
<label>格式：<input name="new_feed_style" type="text" size="35" onKeyUp="format_preview()" onchange="format_preview()"><br />指令包括<br />
标题:[title] <br />
链接:[link] <br />
简介:[desc] <br />
自定义字符串:[msg:字符串] <br />
使用+号相连接。别用空格呀！除了[msg:字符串]外只能用一次。样例：<br />
[desc]<br />
[title]+[link]<br />
[msg:快讯:]+[title]+[desc]+[link]<br />
[msg:快讯:]+[title]+[msg:  ]+[desc]+[link]<br />
</label><br/>
<span name="new_feed_style_preview"></span><br />
<button value="submit" onclick="add_new_feed()">增加</button><br />
<button onclick='set_login_time_out("+")'>如果无法登陆（网络太慢）请点此增加延时。</button><br />
<button onclick='set_login_time_out("-")'>恢复默认延时设置（8秒）</button><span id="time_out"></span><br />
<a href="./submit.html">提交反馈</a><span>   im007boy@gmail.com</span>
</div>
<script type="text/javascript">
function trackButton(button_id,status) {
    _gaq.push(['_trackEvent','Rss_Set', button_id, status]);
  };
</script>

</body>
</html>

